/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ControlAdmin;

import Model.Customer;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.util.locale.StringTokenIterator;

/**
 *
 * @author user
 */
public class ControlAddCustomer extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String birthday = request.getParameter("birthday");
        String idCard = request.getParameter("idCard");
        String address = request.getParameter("address");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String career = request.getParameter("career");
        int status = 0;
        if (name.equals("")) {
            request.setAttribute("errorname", "Bạn chưa nhập tên của khách hàng");
            status = 1;
        }
        if (birthday.equals("")) {
            request.setAttribute("errorbirthday", "Bạn chưa nhập ngày sinh khách hàng");
            status = 1;
        }
        if (idCard.equals("")) {
            request.setAttribute("erroridcard", "Bạn chưa nhập CMND khách hàng");
            status = 1;
        }
        if(address.equals("")){
            request.setAttribute("erroraddress", "Bạn chưa nhập địa chỉ khách hàng");
            status = 1;
        }
        if(email.equals("")){
            request.setAttribute("erroremail", "Bạn chưa nhập địa chỉ email khách hàng");
            status = 1;
        }
        if(phone.equals("")){
            request.setAttribute("errorphone", "Bạn chưa nhập số điện thoại khách hàng");
            status = 1;
        }
        if(status ==1){
            RequestDispatcher dispathcer = getServletContext().getRequestDispatcher("/admin/addcustomer.jsp");
            dispathcer.forward(request, response);
        }
        if(status==0){
           
            StringTokenizer strtoken = new StringTokenizer(birthday, "/");
                        
                int month= Integer.parseInt(strtoken.nextToken());
                int day = Integer.parseInt(strtoken.nextToken());
                int year = Integer.parseInt(strtoken.nextToken());
            System.out.println("Năm"+year );
            Customer newCustomer = Customer.saveCustomer(name, gender.charAt(0),new Date(year, month-1, day),idCard, address, email, phone, career);
            request.setAttribute("customer", newCustomer);
             RequestDispatcher dispathcer = getServletContext().getRequestDispatcher("/admin/viewinfocustomer.jsp");
            dispathcer.forward(request, response);
            
        }
        
                
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
